package yt.kpool.activity.setting;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import yt.kpool.R;
import yt.kpool.bean.BaseBean;
import yt.kpool.network.HttpUtils;
import yt.kpool.network.MyGson;
import yt.kpool.network.NetHelperNew;
import yt.kpool.util.NetWorkUtil;
import yt.kpool.util.TimeCountUtil;
import yt.kpool.util.TitleControl;
import yt.kpool.util.ToastUtil;
import yt.kpool.view.dialog.LoadingDialog;
import yt.kpool.view.dialog.StopPayDialog;
import yt.kpool.view.dialog.SuccessDialog;

/**
 *修改钱包地址
 */
public class ReviseAddressAc extends AppCompatActivity {

    @BindView(R.id.tv_name)
    TextView tv_name;
    @BindView(R.id.edit_address)
    EditText edit_address;
    @BindView(R.id.tv_verificationcode)
    TextView tv_verificationcode;
    @BindView(R.id.edit_code)
    EditText edit_code;
    private TimeCountUtil timeCountUtil;//时间计时
    private LoadingDialog loadingDialog;
    public static void start(Activity mactivity, String ChildAccountID, String CurrencyID, String CurrencyCode, String Address) {
        Intent intent = new Intent(mactivity, ReviseAddressAc.class);
        intent.putExtra("ChildAccountID", ChildAccountID);
        intent.putExtra("CurrencyID", CurrencyID);
        intent.putExtra("CurrencyCode", CurrencyCode);
        intent.putExtra("Address", Address);

        mactivity.startActivityForResult(intent,33);
    }

    public String getChildAccountID() {
        return getIntent().getStringExtra("ChildAccountID");
    }

    public String getCurrencyID() {
        return getIntent().getStringExtra("CurrencyID");
    }
    public String getAddress() {
        return getIntent().getStringExtra("Address");
    }
    public String getCurrencyCode() {
        return getIntent().getStringExtra("CurrencyCode");
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reviseaddress);
        ButterKnife.bind(this);
        initTitle();
        init();

    }
    private void init()
    {
        tv_name.setText("修改"+getCurrencyCode()+"地址");
        edit_address.setText(getAddress());
        timeCountUtil = new TimeCountUtil(ReviseAddressAc.this, 60000, 1000, tv_verificationcode, getResources().getColor(R.color.system_color));
        loadingDialog = new LoadingDialog(this, 0);
    }
    /**
     * 设置标题栏
     */
    private void initTitle() {
        TitleControl t = new TitleControl(this);
        t.setTitle("修改地址");

    }


    /**
     * 点击事件的处理
     *
     * @param view
     */
    @OnClick({ R.id.tv_verificationcode,R.id.btn_add})
    public void onClick(View view) {
        switch (view.getId()) {

            case R.id.tv_verificationcode:
                //获取验证码
                sendSMS();
                break;


            case R.id.btn_add:
                //添加地址
                if (addCheck())
                    addAddress();
                break;
        }
    }

    private boolean addCheck() {
        if ("".equals(edit_address.getText().toString().trim())) {
            ToastUtil.showToast(this, "请填写钱包地址");
            return false;
        }
        if ("".equals(edit_code.getText().toString().trim())) {
            ToastUtil.showToast(this, "请填写短信验证码");
            return false;
        }
        return true;
    }

    /**
     * 添加地址
     */
    private void addAddress() {
        if (NetWorkUtil.check(this)) {

            loadingDialog.show();
            NetHelperNew.ChangeChildAccountAddress("" + getCurrencyID(), "" + getChildAccountID(), edit_address.getText().toString().trim(), edit_code.getText().toString().trim(), new HttpUtils.HttpCallback() {
                @Override
                public void onSuccess(String data) {
                    Log.i("-->", "地址修改：" + data);
                    loadingDialog.dismiss();
                    BaseBean baseBean = MyGson.getInstance().fromJson(data, BaseBean.class);


                    if (baseBean.getType() == 1) {
                        new SuccessDialog(ReviseAddressAc.this, "地址修改成功", new SuccessDialog.OnSuccessListener() {
                            @Override
                            public void getResult() {
                                setResult(11,getIntent());
                               finish();
                            }
                        }).show();

                    } else {
                        ToastUtil.showToast(ReviseAddressAc.this, baseBean.getContent());
                    }
                }

                @Override
                public void onError(String msg) {
                    super.onError(msg);
                    loadingDialog.dismiss();

                    ToastUtil.showToast(ReviseAddressAc.this, msg);
                }
            });

        }


    }

    /**
     * 发送验证码
     */
    private void sendSMS() {
        if (NetWorkUtil.check(this)) {

            //倒计时
            timeCountUtil.start();


            NetHelperNew.ChangeChildAccountAddressSendSmsCode(new HttpUtils.HttpCallback() {
                @Override
                public void onSuccess(String data) {


                    BaseBean baseBean = MyGson.getInstance().fromJson(data, BaseBean.class);
                    Log.i("-->", "修改密码" + baseBean.getContent());
                    if (baseBean.getType() == 1) {

                        ToastUtil.showToast(getApplicationContext(), baseBean.getContent());
                    } else {
                        ToastUtil.showToast(getApplicationContext(), baseBean.getContent());
                    }
                }

                @Override
                public void onError(String msg) {
                    super.onError(msg);
                    ToastUtil.showToast(getApplicationContext(), msg);
                }
            });


        }

    }

}
